package com.example.takeaway.api;

import com.example.takeaway.common.R;
import com.example.takeaway.model.dto.AddCartItemDTO;
import com.example.takeaway.model.vo.CartVO;
import com.example.takeaway.service.CartItemService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 购物车项表 前端控制器
 * </p>
 *
 * @author 邢晨旭
 * @since 2023-10-12
 */
@RestController
@RequestMapping("/cartItem")
public class CartItemApi {

    @Resource
    CartItemService cartItemService;

    @PostMapping
    public R<Void> addCart(@RequestBody AddCartItemDTO param) {
        cartItemService.addCart(param);
        return R.ok();
    }

    @PutMapping("/{id}")
    public R<Void> changeNum(@PathVariable("id") Long id, @RequestParam("num") Integer num) {
        cartItemService.changeNum(id, num);
        return R.ok();
    }

    @DeleteMapping("/{shopId}")
    public R<Void> del(@PathVariable("shopId") Long shopId) {
        //todo 从token中获取用户id
        cartItemService.delCart(shopId, 111L);
        return R.ok();
    }

    @GetMapping("/list/{shopId}")
    public R<CartVO> list(@PathVariable("shopId") Long shopId) {
        //todo 从token中获取用户id
        return R.ok(cartItemService.queryCartList(shopId, 111L));
    }

}
